package com.dyb.comm.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import com.dyb.comm.config.OutputDirectory;
import com.dyb.comm.config.WareParamDTO;
import com.dyb.comm.utils.S3Utils;
import io.minio.*;
import io.minio.errors.*;
import io.minio.messages.DeleteError;
import io.minio.messages.DeleteObject;
import io.minio.messages.Item;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.LinkedList;
import java.util.List;

/**
 * @ClassName : MinioController
 * @Description :
 * @Author : LinYa
 * @Date: 2022/6/23  11:19
 */
@RestController
public class MinioController {

    @Resource
    private MinioClient minioClient;

    @Autowired
    private AmazonS3 s3Client;

    @Autowired
    private OutputDirectory outputDirectory;

    @Value("${minio.bucketName:}")
    private String bucketName;

    @GetMapping("/upload")
    public void upload() throws Exception {
        for (int i = 0; i < 20; i++) {
            ObjectWriteResponse response = minioClient.uploadObject(UploadObjectArgs.builder()
                    .bucket(bucketName)
                    .object("a9482/90235ffs3" + i + "/12" + i + ".jpg")
                    .filename("D:\\WorkSpace\\easyexcel\\minioClient\\壁纸.jpg")
                    .build());
            System.out.println(response.etag());
        }

    }

    @GetMapping("/putObj")
    public void putObj() throws Exception {
        FileInputStream inputStream = new FileInputStream("D:\\WorkSpace\\easyexcel\\minioClient\\壁纸.jpg");

        minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object("a9482/90235ffs3/1234.jpg")
                .stream(inputStream, inputStream.available(), -1)
                .contentType("video/mp4")
                .build());
    }

    @GetMapping("/downLoad")
    public void downLoad(@RequestParam String name) throws Exception {
        minioClient.downloadObject(
                DownloadObjectArgs.builder()
                        .bucket(bucketName)
                        .object(name)
                        .filename("D:\\WorkSpace\\"+ System.currentTimeMillis() +".csv")
                        .build());
    }

    @GetMapping("/test")
    public void test() throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        Iterable<Result<Item>> myObjects = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).prefix("a9482/90235ffs3/").build());
//		Iterable<Result<Item>> myObjects = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).build());
        List<DeleteObject> deleteObjects = new LinkedList<>();

        for (Result<Item> result : myObjects) {
            Item item = result.get();
            System.out.println(item.lastModified() + ", " + item.size() + ", " + item.objectName());
            deleteObjects.add(new DeleteObject(item.objectName()));
        }

        Iterable<Result<DeleteError>> results = minioClient.removeObjects(RemoveObjectsArgs.builder().bucket(bucketName).objects(deleteObjects).build());
        for (Result<DeleteError> result : results) {

        }

    }

    @GetMapping("/s3test")
    public void s3test() {

//        ObjectListing objectListing = s3Client.listObjects(bucketName, "a9482/90235ffs3");
        ObjectListing objectListing = s3Client.listObjects(bucketName, "a9482/");
        System.out.println(objectListing.getObjectSummaries());

//        List<S3ObjectSummary> objectSummaries = objectListing.getObjectSummaries();
//        for (S3ObjectSummary s3ObjectSummary : objectSummaries) {
//            String key = s3ObjectSummary.getKey();
//            s3Client.deleteObject(bucketName, key);
//        }

    }

    @GetMapping("/testout")
    public void testout() {
        WareParamDTO wareParamDTO = new WareParamDTO();
        wareParamDTO.setOutputDirectory(outputDirectory);
        WareParamDTO thisDto = new WareParamDTO();
        BeanUtils.copyProperties(wareParamDTO, thisDto);
        OutputDirectory outputDirectory1 = new OutputDirectory();
        System.out.println(outputDirectory1);
        System.out.println(JSONUtil.toJsonStr(wareParamDTO));
        System.out.println(JSONUtil.toJsonStr(thisDto));
        System.out.println(JSON.toJSONString(wareParamDTO, SerializerFeature.WriteMapNullValue));

    }
}
